import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { UserListDetailBean, PartyListDetailBean } from '../bean/register-check.bean';
import { NzModalService, NzNotificationService, UploadFile } from 'ng-zorro-antd';
import { HttpServcie } from '../../../service/http.service';
import { Config } from "../../../service/config";
import * as moment from 'moment';

@Component({
  selector: 'register-check-detail',
  templateUrl: 'register-check-detail.html',
  styleUrls: ['register-check-detail.scss']
})
export class Checkdetail implements OnInit {
  @Input() inValue: any = [];
  @Output() outValue = new EventEmitter();
  userListDetailBean: UserListDetailBean = new UserListDetailBean();//个人
  partyListDetailBean: PartyListDetailBean = new PartyListDetailBean();//单位
  showFlag: boolean;
  //省市区
  options: any = [];
  addresOptions: any;
  // closeFlag:boolean=true;//关闭按钮
  selectValue;
  previewImage = '';
  previewVisible = false;
  config: any = Config;

  constructor(
    private confirmServ: NzModalService,
    private httpServcie: HttpServcie,
    private _notification: NzNotificationService,
  ) { }

  ngOnInit() {
    //获取用户信息判断是超级管理员还是个人用户还是单位
    if (this.inValue) {
      let user: string = window.localStorage.getItem('user');
      if (user) {
        let userObject = JSON.parse(user);
        //管理员看企业的
        if (userObject['type'] != 'party' && userObject['type'] != 'user') {
          this.selectValue = 'company';
          this.partyListDetailBean = Object.assign({}, this.inValue);
          // 图片回显            
          this.partyListDetailBean.businessLicenseImg = [{ url: `${this.config.IP + this.config.API.getUpload}/${this.inValue.businessLicenseImg}`, name: `${this.inValue.businessLicenseImg}` }];

          if (this.partyListDetailBean.zzzs && this.partyListDetailBean.zzzs.length > 0) {
            let files = this.partyListDetailBean.zzzs.split(',');
            this.partyListDetailBean.zzzs = [];
            for(let i=0;i<files.length;i++){
              this.partyListDetailBean.zzzs.push({ url: `${this.config.IP + this.config.API.getUpload}/${files[i]}`, name: `${files[i]}` });
            }
            /*files.forEach(fileurl => {

              console.log({ url: `${this.config.IP + this.config.API.getUpload}/${fileurl}`, name: `${fileurl}` });
              this.partyListDetailBean.zzzs.push({ url: `${this.config.IP + this.config.API.getUpload}/${fileurl}`, name: `${fileurl}` });
            });*/
          }
          //省市区
          let cityList: string = "";
          Config.CITY.forEach(element => {
            if (this.partyListDetailBean.partyProvince == element.value) {
              cityList += element.label;
            }
            if (this.partyListDetailBean.partyCity == element.value) {
              cityList += element.label;
            }
            if (this.partyListDetailBean.partyArea == element.value) {
              cityList += element.label;
            }
          });
          this.addresOptions = cityList;
        }
        //企业看用户的
        if (userObject['type'] == 'party') {
          this.selectValue = 'personal';
          if (this.inValue) {
            this.userListDetailBean = Object.assign({}, this.inValue);
            // 图片回显
            if (this.inValue.faceUrl) {
              this.userListDetailBean.faceUrl = [{ url: `${this.config.IP + this.config.API.getUpload}/${this.inValue.faceUrl}`, name: `${this.inValue.faceUrl}` }];
            }
            else {
              this.userListDetailBean.faceUrl = null;
            }
            //手写签名回显
            if (this.userListDetailBean.signImg){
              this.userListDetailBean.signImg = [{ url: `${this.config.IP + this.config.API.getUpload}/${this.inValue.signImg}`, name: `${this.inValue.signImg}` }];
            }
            if (this.userListDetailBean.fileList && this.userListDetailBean.fileList.length > 0) {
              let files = this.userListDetailBean.fileList.split(',');
              this.userListDetailBean.fileList = [];
              files.forEach(fileurl => {
                this.userListDetailBean.fileList.push({ url: `${this.config.IP + this.config.API.getUpload}/${fileurl}`, name: `${fileurl}` });
              });
            }

            if (this.inValue.firstgt) {
              this.userListDetailBean.firstgt = moment(this.userListDetailBean.firstgt).format('YYYY-MM-DD');;
            }
            //用户类型
            this.httpServcie.roleQueryListAll({}).subscribe(data => {
              if (data.status == '200') {
                data.data.forEach(element => {
                  if (this.userListDetailBean.roleId == element.id) {
                    this.userListDetailBean.roleId = element.roleName;
                  }
                });
              }
            });
            //单位名称
            this.httpServcie.partyQueryById({ id: this.userListDetailBean.partyId }).subscribe(data => {
              if (data.status == '200') {
                this.userListDetailBean.partyName = data.data.partyName;
              }
            })
          }
        }
        if (this.inValue.status == '2' || this.inValue.status == '3') {
          this.showFlag = true;
        } else if (this.inValue.status == '1') {
          this.showFlag = false;
        }
      }
    }
    let permission = window.localStorage.getItem("funcPermissions");
  }

  loadData(e: { option: any, index: number, resolve: Function, reject: Function }): void {

    if (e.index === -1) {
      e.resolve(this.httpServcie.getProvince());
      return;
    }


    const option = e.option;
    option.loading = true;
    if (e.index === 0) {
      let values: any = option['value'];
      let valueList = this.httpServcie.getCity(e.option);
      let cities: any = {};
      cities[values] = valueList;

      setTimeout(() => {
        option.loading = false;
        e.resolve(cities[option.value]);
      }, 1);
    }
    if (e.index === 1) {
      let value = option['value'];
      let valueList = this.httpServcie.getRelation(e.option);
      let scenicspots: any = {};
      scenicspots[value] = valueList;
      setTimeout(() => {
        option.loading = false;
        e.resolve(scenicspots[option.value]);
      }, 1, scenicspots, option);
    }
  }

  //返回
  back() {
    this.outValue.emit(false);
  }

  //提交
  submit() {
    //如果是企业
    document.getElementsByTagName('button').item(0).disabled = true;
    if (this.selectValue == 'company') {
      let param;
      if (this.partyListDetailBean.status == '3' && !this.partyListDetailBean.remarks) {
        this._notification.create('warning', '警告提示', "备注不能为空");
        document.getElementsByTagName('button').item(0).disabled = false;
        return;
      }
      // 图片数据格式化
      let imgUrl = Object.assign([], this.partyListDetailBean.businessLicenseImg);
      param = Object.assign({}, this.partyListDetailBean);
      param.businessLicenseImg = imgUrl[0].name;

      if (this.partyListDetailBean.zzzs != null && this.partyListDetailBean.zzzs.length > 0) {
        let zzzs = [];
        for (let i = 0; i < this.partyListDetailBean.zzzs.length; i++) {
          zzzs.push(this.partyListDetailBean.zzzs[i].name);
        }
        param.zzzs = zzzs.join(",");
      }
      this.httpServcie.partyCheck(param).subscribe(data => {
        if (data.status == '200') {
          this.outValue.emit(false);
          return;
        }
        this._notification.create('error', '错误提示', data.message);
        document.getElementsByTagName('button').item(0).disabled = false;
      })
    } else {
      if (this.userListDetailBean.status == '3' && !this.userListDetailBean.remarks) {
        this._notification.create('warning', '警告提示', "备注不能为空");
        document.getElementsByTagName('button').item(0).disabled = false;
        return;
      }
      let param;
      // 图片数据格式化
      let faceUrl = Object.assign([], this.userListDetailBean.faceUrl);
      let fileList = Object.assign([], this.userListDetailBean.fileList);
      let signImg = Object.assign([], this.userListDetailBean.signImg);
      param = Object.assign({}, this.userListDetailBean);
      if (faceUrl && faceUrl.length > 0) {
        param.faceUrl = faceUrl[0].name;
      } else {
        param.faceUrl = null;
      }
      if (fileList && fileList.length > 0) {
        param.fileList = fileList[0].name;
      } else {
        param.fileList = null;
      }
      if(signImg && signImg.length > 0){
        param.signImg = signImg[0].name;
      }
      console.log(param)
      this.httpServcie.userCheck(param).subscribe(data => {
        if (data.status == '200') {
          this.outValue.emit(false);
          return;
        }
        this._notification.create('error', '错误提示', data.message);
        document.getElementsByTagName('button').item(0).disabled = false;
      })
    }
  }

  // 预览
  handlePreview = (file: UploadFile) => {
    this.previewImage = file.url || file.thumbUrl;
    this.previewVisible = true;
  }

  // 不允许删除
  nzRemoveFile = (file: UploadFile) => {
    return false;
  }
}
